import requests
import json
from urllib import parse


class AbstractTestCase:

    def do_get(self, api_url, params):
        json_str = parse.quote(json.dumps(params))

        url = 'http://127.0.0.1:8080/{}'.format(api_url)
        result = requests.get('{}?json={}'.format(url, json_str)).json()

        return result

    def do_post(self, api_url, params, files=None, with_json=False):
        url = 'http://127.0.0.1:8080/{}'.format(api_url)

        if with_json:
            params = json.dumps(params)
            result = requests.post(url=url, headers={'Content-Type': 'application/json'}, data=params).json()
        elif files is None:
            result = requests.post(url=url, params=params).json()
        else:
            result = requests.post(url=url, data=params, files=files).json()

        return result
